import Taddress from '@/components/threeAddress.vue'
export default {
  components: {
    Taddress,
  },
  data() {
    return {
      active: '0',
      address: '',//街道
      tabList: [
        {
          name: '家',
          id: '1',
        },
        {
          name: '公司',
          id: '2'
        }
      ],

      checked: false,
      showdz: false,
      taddress: {
        area: '',
        city: '',
        province: ''
      },
      name: '',
      tel: '',
      tabs: 0,
      checkedValue: 0,
      addList: {
  		  area: '',
  		  city: '',
  		  province: ''
      },
      listLength: 0,
    }
  },
  created() {
    this.tab()
  },
  onLoad(options){
    this.listLength=options.length
    // console.log(options.length)
    // console.log(this.$store.state)
  },
  methods: {
    input(){
		  if (this.tel.trim().length !== 11) return false
		  // console.log(this.mobile)
		  if (!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.tel))) {
			  this.$u.toast('请输入正确的手机号')
		  }
    },
    change(status) {
      // console.log(status)
      this.checkedValue = status
      console.log(status)
    },
    tab(id) {
      // 单选
      if (id !== this.active) {
        this.active = id
      } else{
        this.active = -1
        id=0
      }
      console.log(id)
      this.tabs = id
    },
    //新增
    async addAddress() {
  		if(this.name === ''){
  			this.$u.toast('请输入收货人')
  			return false
  		}if(!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.tel))){
        this.$u.toast('请输入正确的手机号')
        return false
      }else if(this.taddress.province === ''){
  			this.$u.toast('请输入收获地址')
  			return false
  		}else if(this.address===''){
  			this.$u.toast('请输入街道')
  			return false
  		}
      try {
        let data = {
          'name': this.name,
          'mobile': this.tel,
          'province': this.taddress.province,
          'city': this.taddress.city,
          'district': this.taddress.area,
          'address': this.address,
          'address_label': this.tabs,
          'isdefault': this.checkedValue,
  				'app_type': 0
        }
        const res = await this.$api.common.addAddressList(data)
        console.log(res)
  			if(res.code === 200){
  				this.$u.toast('新增成功')
  				setTimeout(res => {
  				  uni.$emit('addressChange', true)
  				  uni.navigateBack({
  				    delta: 1
  				  })
  				},1000)
  			}

      } catch (e) {
        console.log(e)
      }
    },
    confirmdz(e) {
      console.log(e)
      this.addList.area = e.area.label //区
      this.addList.city = e.city.label //市
      this.addList.province = e.province.label //省
      this.taddress.area = Number(e.area.value) //区
      this.taddress.city = e.city.id //市
      this.taddress.province = e.province.id //省
      this.showdz = false
    },
    close() {
      this.showdz = false
    }
  }
}
